CC  := gcc
CXX := g++ 
DEL := rm
CP  := cp
argfunc = $(subst /,\,$(1))

.PHONY: all tools clean

EXESUFFIX    := .exe

.SILENT : all

all: create_dirs bin/teeworlds$(EXESUFFIX) bin/teeworlds_srv$(EXESUFFIX) tools
	echo "Done all!"

debug: all

###
# Collect object files to build
#

collect = $(foreach v,$(1), $(subst src/,bin/, $(addsuffix .o, $(basename $(wildcard $v)))))

base           			:= $(call collect, src/base/*.c)
engine         			:= $(call collect, src/engine/*.c)
engine_client 			:= $(call collect, src/engine/client/*.c)
zlib          			:= $(call collect, src/engine/external/zlib/*.c)
pnglite        			:= $(call collect, src/engine/external/pnglite/*.c)
game          			:= $(call collect, src/game/*.cpp)
game_client    			:= $(call collect, src/game/client/*.cpp)
game_client_components	:= $(call collect, src/game/client/components/*.cpp)
editor        			:= $(call collect, src/game/editor/*.cpp)
server         			:= $(call collect, src/game/server/*.cpp)
server_entities         := $(call collect, src/game/server/entities/*.cpp)
server_gamemodes        := $(call collect, src/game/server/gamemodes/*.cpp)
engine_server  			:= $(call collect, src/engine/server/*.c)
wav_pack       			:= $(call collect, src/engine/external/wavpack/*.c)
tools          			:= $(call collect, src/tools/*.c src/tools/*.cpp)

create_dirs :
	mkdir -p bin/base
	mkdir -p bin/engine/client
	mkdir -p bin/engine/external/zlib
	mkdir -p bin/engine/external/pnglite
	mkdir -p bin/game/client/components
	mkdir -p bin/game/editor
	mkdir -p bin/game/server/entities
	mkdir -p bin/game/server/gamemodes
	mkdir -p bin/engine/server
	mkdir -p bin/engine/external/wavpack
	mkdir -p bin/tools
	mkdir -p bin/game/generated

###
# Build rules
#

bin/engine/external/wavpack/%.o : src/engine/external/wavpack/%.c 
	$(CC) -c $< -o $@

bin/%.o : src/%.c 
	$(CC) -Isrc -Isrc/engine/external/pnglite -Isrc/engine/external/wavpack -Isrc/engine/external/zlib -Iother/sdl/include -Iother/mingw -O0 -g3 -Wall -c -fmessage-length=0 -c -Isrc $< -o $@

bin/%.o : src/%.cpp
	$(CXX) -Isrc -Isrc/engine/external/pnglite -Isrc/engine/external/wavpack -Isrc/engine/external/zlib -Iother/sdl/include -Iother/mingw -O0 -g3 -Wall -c -fmessage-length=0 $< -o $@

###
# Stuff to be generated by python scripts
#

src/game/generated/g_protocol.cpp :
	python "datasrc/compile.py" network_source > $@
	
src/game/generated/g_protocol.hpp :
	python "datasrc/compile.py" network_header > $@

src/game/generated/gc_data.cpp:
	python "datasrc/compile.py" client_content_source > $@
	
src/game/generated/gc_data.hpp:
	python "datasrc/compile.py" client_content_header > $@
	
src/game/generated/gs_data.cpp:
	python "datasrc/compile.py" server_content_source > $@
	
src/game/generated/gs_data.hpp:
	python "datasrc/compile.py" server_content_header > $@

bin/game/generated/g_protocol.o :  src/game/generated/g_protocol.cpp 
	$(CC) -c -Isrc $< -o $@
	
bin/game/generated/gc_data.o :  src/game/generated/gc_data.cpp 
	$(CC) -c -Isrc $< -o $@

bin/game/generated/gs_data.o :  src/game/generated/gs_data.cpp 
	$(CC) -c -Isrc $< -o $@
	
src/game/generated/nethash.c : src/game/generated/g_protocol.hpp src/game/generated/g_protocol.hpp
	python "scripts/cmd5.py" "src/engine/e_protocol.h" "src/game/generated/g_protocol.hpp" "src/game/tuning.hpp" "src/game/gamecore.cpp" src/game/generated/g_protocol.hpp > $@

bin/game/generated/nethash.o :  src/game/generated/nethash.c
	$(CC) -c -Isrc $< -o $@
	

###
# libraries
#

client_libs = gdi32 user32 ws2_32 ole32 shell32 opengl32 glu32 dsound winmm SDL SDLMain
tools_libs  = gdi32 user32 ws2_32 ole32 shell32 opengl32 glu32 dsound winmm SDL SDLMain
server_libs = gdi32 user32 ws2_32 ole32 shell32 opengl32 glu32 winmm 

client_obj = src/game/generated/g_protocol.hpp \
			 bin/game/generated/g_protocol.o \
		     src/game/generated/gc_data.hpp \
		     bin/game/generated/gc_data.o \
		     bin/game/generated/nethash.o \
	    $(base) $(engine) $(engine_client) $(game_client_components) $(zlib) $(pnglite) $(glfw) $(wav_pack) $(game) $(editor) $(game_client) 

server_obj = src/game/generated/g_protocol.hpp \
			 bin/game/generated/g_protocol.o \
		     src/game/generated/gs_data.hpp \
		     bin/game/generated/gs_data.o \
		     bin/game/generated/nethash.o \
	    $(base) $(engine) $(zlib) $(game) $(server) $(server_entities) $(server_gamemodes) $(engine_server)

###
# The various targets
#
bin/teeworlds$(EXESUFFIX): $(client_obj)
	$(CXX) $(filter %.o, $^) $(filter %.a, $^) -Lother/sdl/mingw $(foreach l,$(client_libs), -l$l) -o $@

bin/teeworlds_srv$(EXESUFFIX): $(server_obj)
	$(CXX) $(filter %.o, $^) $(filter %.a, $^) -Lother/sdl/mingw $(foreach l,$(server_libs), -l$l) -o $@


#tools
bin/%$(EXESUFFIX) : bin/tools/%.o $(base) $(engine) $(zlib) 
	$(CXX) $^ -Lother/sdl/mingw $(foreach l,$(tools_libs), -l$l) -o $@
	
clean: 
	-@$(DEL) -rf bin/*
	-@$(DEL) src/game/generated/*
	@echo "Cleaned!"
	
tools: $(addprefix bin/, $(addsuffix $(EXESUFFIX), $(notdir $(basename $(tools)))))

